var DBFactory = require('../db/mysql_db');
var cartDao = module.exports;

cartDao.list = function(data, cb) {
    if(!data) return cb(new ERR(500));
    var sql_total = 'select count(*) as count from shop_cart where uid = ? ';
    DBFactory.poolQuery(sql_total, [data.uid, data.start, data.size],
        function(){
            cb('no collection');
        },function (err,total) {
            if(err){
                cb(err);
                console.log('total Err: ' + err);
                return;
            }
            var sql = 'select sc.id as cart_id, sc.uid, sc.gid, sc.num, sg.pic_url, sg.title, ' +
                ' sg.total_num, sg.score, sg.content from shop_cart as sc left join shop_goods ' +
                ' as sg on sc.gid = sg.id where sc.uid = ? limit ?, ? ';
            DBFactory.poolQuery(sql, [data.uid, data.start, data.size],
                function(){
                    cb('no collection');
                },function (err,result) {
                    if(err){
                        cb(err);
                        console.log('list Err: ' + err);
                        return;
                    }
                    var cartInfo = {total: total[0].count};
                    cartInfo.data = result;
                    cb(null, cartInfo);
                }
            );
        }
    );
}

function existCart(data, cb) {
    var sql = 'select count(*) as count from shop_cart where uid = ? and gid =? ';
    DBFactory.poolQuery(sql, [data.uid, data.gid],
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('existCart Err: ' + err);
                return;
            }
            var isExist = result[0].count > 0 ? true : false;
            cb(null, isExist);
        }
    );
}

cartDao.save = function(data, cb) {
    if(!data) return cb(new ERR(500));
    if(data.id) {
        updateCart(data, cb);
    } else {
        existCart(data, function(err, exist){
            if(err) {
                cb('加入购物车失败！');
                return;
            } else {
                if(!exist) {
                    var sql = 'replace into shop_cart set ? ';
                    DBFactory.poolQuery(sql, data,
                        function(){
                            cb('no collection');
                        },function (err,result) {
                            if(err){
                                cb(err);
                                console.log('save Err: ' + err);
                                return;
                            }
                            cb(null, '加入购物车成功！');
                            return;
                        }
                    );
                } else {
                    var sql = 'update shop_cart set num = num +1 where uid = ? and gid = ?';
                    DBFactory.poolQuery(sql, [data.uid, data.gid],
                        function(){
                            cb('no collection');
                        },function (err,result) {
                            if(err){
                                cb(err);
                                console.log('save Err: ' + err);
                                return;
                            }
                            cb(null, '加入购物车成功！');
                        }
                    );
                }
            }
        });
    }
}

function updateCart(data, cb) {
    if(!data) return cb(new ERR(500));
    var arr = [];
    if(parseInt(data.num) == 0) {
        var sql = 'delete from shop_cart where uid = ? and id = ?';
        arr.push(data.uid);
        arr.push(data.id);
    } else {
        sql = 'update shop_cart set num = ? where uid = ?  and id = ?';
        arr.push(data.num);
        arr.push(data.uid);
        arr.push(data.id);
    }
    DBFactory.poolQuery(sql, arr,
        function(){
            cb('no collection');
        },function (err,result) {
            if(err){
                cb(err);
                console.log('updateCart Err: ' + err);
                return;
            }
            cb(null, '编辑成功！');
        }
    );
}

cartDao.delete = function(data, cb) {
    if(!data) return cb(new ERR(500));
    var sql = 'delete from shop_cart where uid = ? and id in( ? )';
    DBFactory.poolQuery(sql, [data.uid, data.idList],
        function(){
            cb('no collection');
        },function (err) {
            if(err){
                cb(err);
                console.log('delete Err: ' + err);
                return;
            }
            cb(null, '删除成功！');
        }
    );
}